package com.yongcheon.sms_manager.activities;

import android.app.ActionBar;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;

import com.yongcheon.sms_manager.R;
import com.yongcheon.sms_manager.data.preference.SpamSentenceData;
import com.yongcheon.sms_manager.database.SpamDBManager;
import com.yongcheon.sms_manager.ui.dialogs.SpamSenetnceRegistDlg;
import com.yongcheon.sms_manager.ui.preference.SpamSentenceCursorAdapter;
import com.yongcheon.sms_manager.vo.SpamSentenceVO;

/**
 * Created by yongcheon on 2014-12-12.
 */
public class SpamSentenceActivity extends Activity {
    private SpamSentenceCursorAdapter cursorAdapter;

    @Override
    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setTitle(this.getResources().getString(R.string.spam_sentence_management));
        setContentView(R.layout.spam_sentence_list);
        setActionBar();
        setAdapter();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
//        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu_preference_spam_num, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
            case R.id.add_custom_spam_num:
                View view = (View) findViewById(R.id.spam_centence_list);
                SpamSenetnceRegistDlg dlg = new SpamSenetnceRegistDlg();
                dlg.showRegSentenceDlg(this, view, null, this);
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    public void setActionBar() {
        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);
    }

    public void setAdapter(){
        ListView listView = (ListView)findViewById(R.id.spam_centence_list);
        SpamSentenceData spamSentenceData = new SpamSentenceData(this);
        SQLiteDatabase database = SpamDBManager.getDatabase(this);
        Cursor cursor = spamSentenceData.select(null, this, database);
        cursorAdapter = new SpamSentenceCursorAdapter(this, cursor);
        cursorAdapter.setSpamNumberData(spamSentenceData);
        listView.setAdapter(cursorAdapter);

    }

    public void addSentence(SpamSentenceVO spamentenceVO){
        SpamSentenceData spamSentenceData = new SpamSentenceData(this);
        SQLiteDatabase database = SpamDBManager.getDatabase(this);
        spamSentenceData.insertNotExist(spamentenceVO, this, database);
        cursorAdapter.getCursor().requery();
    }

}
